<?php 

defined('G_IN_SYSTEM')or exit('No permission resources.');
ini_set("display_errors","OFF");
class paypal_url extends SystemAction {
	public function __construct(){			
		$this->db=System::load_sys_class('model');		
	} 	
	
	public function qiantai(){	
//		sleep(2);
//		$out_trade_no = $_GET['out_trade_no'];	//商户订单号
//		$dingdaninfo = $this->db->GetOne("select * from `@#_member_addmoney_record` where `code` = '$out_trade_no'");
//		if(!$dingdaninfo || $dingdaninfo['status'] == '未付款'){
//			_message("支付失败");
//		}else{
//			if(empty($dingdaninfo['scookies'])){
//				_message("充值成功!",WEB_PATH."/member/home/userbalance");
//			}else{
//				if($dingdaninfo['scookies'] == '1'){
//					_message("支付成功!",WEB_PATH."/member/cart/paysuccess");
//				}else{
//					_message("commodity还未购买,请重新购买commodity!",WEB_PATH."/member/cart/cartlist");
//				}
//
//			}
//		}
	}
	
	public function houtai(){
        if (isset($_GET['success']) && $_GET['success'] == 'true') {

            $pay_type =$this->db->GetOne("SELECT * from `@#_pay` where `pay_class` = 'paypal' and `pay_start` = '1'");
            $pay_type_key = unserialize($pay_type['pay_key']);
            $clientId =  $pay_type_key['id']['val'];
            $clientSecret =  $pay_type_key['key']['val'];

            $apiContext = new \PayPal\Rest\ApiContext(
                new \PayPal\Auth\OAuthTokenCredential(
                    $clientId,
                    $clientSecret
                )
            );
            // TODO 正式的Time候需要关闭
            $apiContext->setConfig(
                array(
                    'mode' => 'sandbox',
                    'log.LogEnabled' => true,
                    'log.FileName' => 'system/caches/PayPal.log',
                    'log.LogLevel' => 'DEBUG',
                    'cache.enabled' => true
                )
            );

            $paymentId = $_GET['paymentId'];
            $payment = \PayPal\Api\Payment::get($paymentId, $apiContext);

            $execution = new \PayPal\Api\PaymentExecution();
            $execution->setPayerId($_GET['PayerID']);

            $transaction = new \PayPal\Api\Transaction();
            $amount = new \PayPal\Api\Amount();
            $details = new \PayPal\Api\Details();

            $details->setShipping(1)
                ->setTax(2)
                ->setSubtotal(7.8);

            $amount->setCurrency('USD');
            $amount->setTotal(10.8);
            $amount->setDetails($details);
            $transaction->setAmount($amount);

            $execution->addTransaction($transaction);

            try {
                $result = $payment->execute($execution, $apiContext);
                echo "支付成功";
            } catch (Exception $ex) {
                echo "支付失败";
                die;
            }

            return $payment;
        } else {
            echo "PayPalreturn回调地址参数错误";
        }
	}
	
}//

?>